

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'root',
  database : 'zufang'
});
connection.connect();
var datas={"code":0,"data":'',"msg":''};
exports.login=function(user,callback){
 var sql="SELECT * from user_info where name='"+user.name+"' and password='"+user.password+"'";
  connection.query(sql,function (err, result) {
    if(err){
      datas.msg=err.message;
      callback(datas)
    }else{
      callback(datas)
    }
  });
};

/**
 * 获取列表
 * @param  {Function} callback 回调函数
 */
exports.find = function (callback) {
	connection.query('SELECT * from zufang', function (error, results, fields) {
	  callback(results)
	});
};

exports.saveFaBu=function(zufang,callBack){
    var sql="INSERT INTO `zufang`( `img`, `title`, `content`, `price`, `address`) VALUES (?, ?, ?, ?, ?);";
    var  addSqlParams = [zufang.img, zufang.title,zufang.content,zufang.price,zufang.address];
    connection.query(sql,addSqlParams,function (err,results) {
        callBack(null);
    })
}





/**
 * 添加保存
 * @param  {Object}   user  对象
 * @param  {Function} callback 回调函数
 */
exports.save = function (user, callback) {
  var sql="INSERT INTO user_info( `name`, `password`,phone,nickName) VALUES (?,?,?,?)";
  var  addSqlParams = [user.name, user.password,user.phone,user.nickName];
  connection.query(sql,addSqlParams,function (err,results) {
    callback(null);
  })
}


/**
 * 删除
 */
exports.deleteById = function (id, callback) {
  var sql="DELETE from zufang where id="+id;
  connection.query(sql,function (err,result) {
    callback(null);
  })
}
